<template>
  <div>
    <span>foreach循环了多少次：{{ foreachCount }}</span>
    <span>some循环了多少次：{{ someCount }}</span>
    <SPAN>every所有城市名称都是2个字:{{ everyBool }}</SPAN>
    <span>reduce结果：{{ cityCount }}</span>
  </div>
</template>
<script>
export default {
  name: 'ArrayCom',
  data () {
    return {
      foreachCount: 0,
      someCount: 0,
      everyBool: false,
      cityCount: 0
    }
  },
  created () {
    const list = ['西安', '北京', '武汉', '苏州', '荆门']
    list.forEach((value) => {
      this.foreachCount++
      if (value === '武汉') {
        console.log('foreach循环无法提前结束循环')
        // return无效
      }
    })
    list.some((value) => {
      this.someCount++
      if (value === '武汉') {
        console.log('some循环可以提前结束循环')
        return true
      }
    })
    this.everyBool = list.every((value) => {
      return value.length === 2
    })
    this.cityCount = list.reduce(
      (previousValue, currentValue, currentIndex) => {
        console.log('previousValue:', previousValue)
        console.log('currentValue:', currentValue)
        console.log('currentIndex:', currentIndex)
        return previousValue + currentValue.length
      },
      0
    )
  }
}
</script>
